<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : Article.php
 * Created on : 2022-12-22 10:59:07
 * Author     : Tiger <1192851302@qq.com>
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace app\admin\controller;

use app\Request;
use haoteam\service\UtilService;
use haoteam\service\DataExtend;
use app\models\ArticleCategory;
use app\models\Article as ArticleModel;
use haoteam\http\validates\ArticleCategoryValidate;
use haoteam\http\validates\ArticleValidate;

/**
 * 内容管理
 * Class Article
 * Tiger <1192851302@qq.com> 2022-12-22 10:59:07
 */
class Article {

    /**
     * 类别管理
     * @auth true
     * @menu true
     * @author Tiger <1192851302@qq.com>
     * @desc    内容类别管理
     * @param Request $request
     * @return type
     */
    public function category_index() {
        $data = ArticleCategory::select()->toArray();
        foreach ($data as &$vo) {
            $vo['ids'] = join(',', DataExtend::getArrSubIds($data, $vo['id']));
        }
        return app('json')->successful($data);
    }

    /**
     * 类别创建
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @desc    内容类别创建
     * @param Request $request
     * @return type
     */
    public function category_create() {
        $post = UtilService::postMore([
                    'pid',
                    'name'
        ]);
        $validate = new ArticleCategoryValidate();
        if ($validate->scene('create')->check($post) == false) {
            return app('json')->fail($validate->getError());
        }
        try {
            $_data = ArticleCategory::create($post)->toArray();
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }

        return app('json')->successful('创建成功', $_data);
    }

    /**
     * 类别更新
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @desc    内容类别更新
     * @param Request $request
     * @return type
     */
    public function category_update() {
        $post = UtilService::postMore([
                    'id',
                    'pid',
                    'name'
        ]);
        $validate = new ArticleCategoryValidate();
        if ($validate->scene('update')->check($post) == false) {
            return app('json')->fail($validate->getError());
        }
        try {
            ArticleCategory::update($post, ['id' => $post['id']]);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }

        return app('json')->successful('编辑成功');
    }

    /**
     * 快速更新类别
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @desc 此处只能更新指定字段
     * @return string msg 结果描述
     */
    public function category_save() {
        $post = UtilService::postMore([
                    ['ids', []],
                    'field',
                    'value'
        ]);
        $validate = new ArticleCategoryValidate();
        if ($validate->scene('save')->check($post) == false) {
            return app('json')->fail($validate->getError());
        }
        try {
            ArticleCategory::whereIn('id', $post['ids'])->update([$post['field'] => $post['value']]);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('修改成功');
    }

    /**
     * 上级类别列表
     * @login true
     * @author Tiger <1192851302@qq.com>
     * @return array
     */
    public function category_parent(Request $request) {
        [$type] = UtilService::postMore([
                    ['type', '']
                        ], $request, true);
        $list = ArticleCategory::where('status', 1)->select()->toArray();
        $extra = [['id' => 0, 'pid' => -1, 'name' => '顶部类别']];
        if ($type == 'publish') {
            $extra = [];
        }
        $_list = DataExtend::arr2table(array_merge($list, $extra));
        foreach ($_list as $key => $menu) {
            if ($menu['spt'] >= 3 && empty($type)) {
                unset($_list[$key]);
            }
        }
        return app('json')->successful($_list);
    }

    /**
     * 内容列表
     * @auth true
     * @menu true
     * @author Tiger <1192851302@qq.com>
     * @desc    内容类别管理
     * @param Request $request
     * @return type
     */
    public function index(Request $request) {
        $post = UtilService::postMore([
                    'search'
        ]);
        $data = ArticleModel::getList($post);
        return app('json')->successful($data);
    }

    /**
     * 内容添加
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @desc    内容管理
     * @param Request $request
     * @return type
     */
    public function create(Request $request) {
        $post = UtilService::postMore([
                    'category_id',
                    'title',
                    'cover_image',
                    'message',
                    'status'
        ]);
        $validate = new ArticleValidate();
        if ($validate->scene('create')->check($post) == false) {
            return app('json')->fail($validate->getError());
        }
        try {
            ArticleModel::create($post);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('发布成功');
    }

    /**
     * 内容更新
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @desc    内容管理
     * @param Request $request
     * @return type
     */
    public function update(Request $request) {
        $post = UtilService::postMore([
                    'id',
                    'category_id',
                    'title',
                    'cover_image',
                    'message',
                    'status'
        ]);
        $validate = new ArticleValidate();
        if ($validate->scene('update')->check($post) == false) {
            return app('json')->fail($validate->getError());
        }
        try {
            ArticleModel::update($post, ['id' => $post['id']]);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('更新成功');
    }

    /**
     * 内容删除
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @desc    内容管理
     * @param Request $request
     * @return type
     */
    public function delete(Request $request) {
        $post = UtilService::postMore([
                    'id'
        ]);
        $validate = new ArticleValidate();
        if ($validate->scene('delete')->check($post) == false) {
            return app('json')->fail($validate->getError());
        }
        try {
            ArticleModel::update(['is_del' => 1], ['id' => $post['id']]);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('删除成功');
    }

}
